<?php

/**
 * Created by PhpStorm.
 * User: boye009
 * Date: 2016/3/16
 * Time: 13:40
 */
namespace app\bbs\api;

use app\bbs\model\ForumPostcomment;
use app\common\api\BaseApi;

class ForumPostcommentApi extends BaseApi{

    /**
     * 添加
     */
    const ADD = "bbs/ForumPostcommentApi/add";
    /**
     * 保存
     */
    const SAVE = "bbs/ForumPostcommentApi/save";
    /**
     * 保存根据ID主键
     */
    const SAVE_BY_ID = "bbs/ForumPostcommentApi/saveByID";

    /**
     * 删除
     */
    const DELETE = "bbs/ForumPostcommentApi/delete";

    /**
     * 查询
     */
    const QUERY = "bbs/ForumPostcommentApi/query";
    const MY_COMMENTS = "bbs/ForumPostcommentApi/queryByUid";
    /**
     * 查询一条数据
     */
    const GET_INFO = "bbs/ForumPostcommentApi/getInfo";

    const QUERY_WITH_COUNT = "bbs/ForumPostcommentApi/queryWithCount";

    /**
     * 统计数量
     */
    const COUNT = "bbs/ForumPostcommentApi/count";

    /**
     * 评论和帖子联表查询
     */
    const QUERY_WITH_POST = "bbs/ForumPostcommentApi/queryWithPost";

    protected function _init(){
        $this -> model = new ForumPostcomment();
    }
    /**
     * 我的评论 分页
     */
    public function queryByUid($uid,$page=1,$size=10,$order='id desc'){
        $where = '`author_id`=\''.$uid.'\'';// and status>=0';
        $page  = intval($page)>1 ?intval($page):1;
        $start = ($page-1)*$size;
        $model = $this->model;
        $count = $model->field('tid')->where($where)->count();
        $list  = $model->where($where)->limit($start,$size)->order($order)->select();
        return $this->apiReturnSuc(['count'=>$count,'list'=>$list]);
    }
    public function queryWithPost($map = null, $page = array('curpage'=>1,'size'=>6), $order = false, $params = false, $fields = false){

        $query = $this->model -> join("itboye_forum_post on itboye_forum_post.pid=itboye_forum_postcomment.pid");
        if(!is_null($map)){
            $query = $query->where($map);
        }
        if(!($order === false)){
            $query = $query->order($order);
        }
        if(!($fields === false)){
            $query = $query->field($fields);
        }
        $list = $query -> page($page['curpage'] . ',' . $page['size']) -> select();
        //return $this -> apiReturnSuc($list);


        if ($list === false) {
            $error = $this -> model -> getDbError();
            return $this -> apiReturnErr($error);
        }

        $count = $this -> model -> join("itboye_forum_post on itboye_forum_post.pid=itboye_forum_postcomment.pid") ->  where($map) -> count();

        // // 查询满足要求的总记录数
        // $Page = new \Think\Page($count, $page['size']);

        // //分页跳转的时候保证查询条件
        // if ($params !== false) {
        //     foreach ($params as $key => $val) {
        //         $Page -> parameter[$key] = urlencode($val);
        //     }
        // }

        // // 实例化分页类 传入总记录数和每页显示的记录数
        // $show = $Page -> show();
        return $this -> apiReturnSuc(array("count" => $count, "list" => $list));
    }


}